package win.timloong.android.carwiki.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import win.timloong.android.carwiki.R;
import win.timloong.android.carwiki.adapter.ItemAdapter;
import win.timloong.android.carwiki.bean.IndexItem;
import win.timloong.android.carwiki.presenter.IndexItemPresenter;
import win.timloong.android.carwiki.view.IIndexItemView;

/**
 * A simple {@link Fragment} subclass.
 */
public class IndexItemFragment extends Fragment implements IIndexItemView {

    RecyclerView recyclerView;
    ItemAdapter adapter;
    private boolean loadingFlag;
    TextView tvMore;

    private IndexItemPresenter presenter;

    private int nextPos;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

           presenter.stop();

        }
    };

    public IndexItemFragment() {
        Log.e("IndexItemFragment", "init");
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        presenter = new IndexItemPresenter(this);

        final View view = inflater.inflate(R.layout.fragment_index_item, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.index_recyclerview);
        tvMore = (TextView) view.findViewById(R.id.index_more);
        ArrayList<IndexItem> arrayList = new ArrayList<>();
        arrayList.add(new IndexItem("标题1", "1", 0, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题2", "2", 1, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题3", "3", 0, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题4", "4", 0, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题5", "5", 1, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题6", "6", 1, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题7", "http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fa4.att.hudong.com%2F23%2F65%2F28300001051406135587658805173_950.jpg&thumburl=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D2552322979%2C1160770908%26fm%3D21%26gp%3D0.jpg", 0, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题8", "6", 1, "Lab8", "1001", "1天前"));
        arrayList.add(new IndexItem("标题9", "6", 1, "adsfhadf挖机费哦", "1001", "1天前"));
        arrayList.add(new IndexItem("标题10", "6", 1, "Lab1", "的顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶", "1天前"));
        arrayList.add(new IndexItem("标题11", "6", 1, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题12", "6", 1, "Lab1", "1001", "1天前"));

        adapter = new ItemAdapter(arrayList);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());

        recyclerView.setLayoutManager(layoutManager);


        recyclerView.setAdapter(adapter);


        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                int itemCount = layoutManager.getItemCount();
                if (lastVisibleItemPosition >= itemCount - 1 && dy > 0) {
                    nextPos = lastVisibleItemPosition + 1;
                    presenter.start();
                }
            }
        });


        return view;
    }

    private void loadData() {
        new Thread() {
            @Override
            public void run() {
                SystemClock.sleep(2000);
                handler.sendEmptyMessage(0);


            }
        }.start();
    }

    @Override
    public void startLoad() {

        if (!loadingFlag) {

            loadData();
            loadingFlag = true;
            tvMore.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void finishLoad() {
        loadingFlag = false;

        ArrayList<IndexItem> arrayList = new ArrayList<>();
        arrayList.add(new IndexItem("标题1", "1", 0, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题2", "2", 1, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题3", "3", 0, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题4", "4", 0, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题5", "5", 1, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题6", "6", 1, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题7", "http://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fa4.att.hudong.com%2F23%2F65%2F28300001051406135587658805173_950.jpg&thumburl=http%3A%2F%2Fimg1.imgtn.bdimg.com%2Fit%2Fu%3D2552322979%2C1160770908%26fm%3D21%26gp%3D0.jpg", 0, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题8", "6", 1, "Lab8", "1001", "1天前"));
        arrayList.add(new IndexItem("标题9", "6", 1, "adsfhadf挖机费哦", "1001", "1天前"));
        arrayList.add(new IndexItem("标题10", "6", 1, "Lab1", "的顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶", "1天前"));
        arrayList.add(new IndexItem("标题11", "6", 1, "Lab1", "1001", "1天前"));
        arrayList.add(new IndexItem("标题12", "6", 1, "Lab1", "1001", "1天前"));
        adapter.addData(arrayList);
    }

    @Override
    public void refreshView() {
        adapter.notifyDataSetChanged();
        tvMore.setVisibility(View.GONE);
    }
}
